package filemanager;

import java.sql.SQLException;

import core.Client;
import dbms.DBParameters;
import dbms.DBSelectingFunctions;

public class ProgramRunner extends Thread {

	String programName;
	String fileName;
	String extenzion;
	
	String mainDir;
	String programDir;
	String executingName;
	String filePath;
	
	public ProgramRunner(String programName, String fileName, String extenzion) throws SQLException
	{
		this.programName = programName;
		this.fileName = fileName;
		this.extenzion = extenzion;
		
		this.mainDir = Client.getInstance().getConfigs().get(DBParameters.MAINDIR);
		this.programDir = DBSelectingFunctions.getProgramParameter(programName, DBParameters.PROGRAMDIR);
		this.executingName = DBSelectingFunctions.getProgramParameter(programName, DBParameters.PROGRAMEXECUTINGNAME);
		this.filePath = mainDir + "/" + programName + "/" + fileName + "." + extenzion;
		
		System.out.println("main dir: " + mainDir);
		System.out.println("program dir: " + programDir);
		System.out.println("file name: " + fileName);
		System.out.println("filepath: " + filePath);
		
	}
	
	@Override
	public void run() {

		try {
			if(DBSelectingFunctions.getProgramParameter(programName, DBParameters.PROGRAMTYPE).equals(DBParameters.EXECUTABLE))
				FileOpening.runFile(this.programDir, this.executingName, this.filePath);
			else
				FileOpening.runJavaFile(this.programDir, this.executingName, this.filePath);
						
			Client.getInstance().closeFile(this.programName, this.fileName, this.extenzion);

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		FileManager.deleteFile(this.filePath);
	}
	
}